package com.example.eos.controller;

import com.example.eos.bean.Label;
import com.example.eos.bean.ReturnInfo;
import com.example.eos.service.GlobalService;
import com.example.eos.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class LabelController {

    private LabelService labelService;
    private GlobalService globalService;

    @Autowired
    public LabelController(LabelService labelService, GlobalService globalService){
        this.labelService = labelService;
        this.globalService = globalService;
    }

    //查询标签列表
    @RequestMapping(value = "/label",method = RequestMethod.GET)
    public ReturnInfo getLabel(){
        return labelService.getLabel();
    }

    //查询标签列表（标签名）
    @RequestMapping(value = "/label/labelname",method = RequestMethod.GET)
    public ReturnInfo getLabelList(@RequestBody @Valid Label label, BindingResult result){
        List<String> stringList = new ArrayList<>();
        if(result.hasErrors()){
            return globalService.returnErrors(result, stringList);
        }else {
            return labelService.getLabelList(label.getLabelname());
        }
    }

    //新建标签
    @RequestMapping(value = "/label",method = RequestMethod.POST)
    public ReturnInfo creatLabel(@RequestBody @Valid Label label, BindingResult result){
        List<String> stringList = new ArrayList<>();
        if(result.hasErrors()){
            return globalService.returnErrors(result, stringList);
        }else {
            return labelService.creatLabel(label, stringList);
        }
    }

    //删除标签
    @RequestMapping(value = "/label/{labelid}", method = RequestMethod.DELETE)
    public ReturnInfo deteleLabel(@PathVariable String labelid){
        return labelService.deteleLabel(labelid);
    }

}
